package com.stox.advice;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Aspect
public class RestControllerLoggingAdvice {
    private static final Logger log = LoggerFactory.getLogger(RestControllerLoggingAdvice.class);

    @AfterReturning(pointcut = "execution(* com.stox.controller.*.*(..))", returning = "returnObject")
    public void logAfter(Object returnObject) {
        if (null != returnObject) {
            log.debug(ToStringBuilder.reflectionToString(returnObject));
        }
    }

    @Before("execution(* com.stox.controller.*.*(..))")
    public void logBefore(JoinPoint joinPoint) {
        Object args[] = joinPoint.getArgs();
        if (null != args && 0 < args.length) {
            for (Object arg : args)
                if (null != arg)
                    log.debug(ToStringBuilder.reflectionToString(arg));
        }
    }
}
